package com.zpark.drug.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.mapper.DrugMapper;
import com.zpark.drug.pojo.Drug;
import com.zpark.drug.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
@Transactional
public class DrugServiceImpl implements DrugService {
    @Autowired
    private DrugMapper drugMapper;
    @Override
    public List<Drug> findAll() {
        return drugMapper.selectAll();
    }

    @Override
    public Drug findById(Integer id) {
        return drugMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addDrug(Drug drug) {
        return drugMapper.insert(drug);
    }

    @Override
    public int updateDrug(Drug drug) {
        return drugMapper.updateByPrimaryKey(drug);
    }

    @Override
    public int deleteDrugById(Integer id) {
        return drugMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Drug> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Drug.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("name", "%" + param + "%") // 使用 orLike 来实现多字段模糊查询
                    .orLike("number", "%" + param + "%")
                    .orLike("formula", "%" + param + "%")
                    .orLike("dosage", "%" + param + "%")
                    .orLike("supplier", "%" + param + "%");
        }
        List<Drug> drugs;
        if (param != null && !param.isEmpty()) {
            drugs = drugMapper.selectByExample(example);
        } else {
            drugs = drugMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(drugs);
    }



//    public Example createExample(Drug drug) {
//        Example example = new Example(Drug.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (drug != null) {
//            //名称
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getId())) {
//                criteria.andLike("id", "%" + drug.getId() + "%");
//            }
//            //品牌的图片地址
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getName())) {
//                criteria.andLike("number", "%" + drug.getNumber() + "%");
//            }
//            //品牌的首字母
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getName())) {
//                criteria.andLike("name", "%" + drug.getName() + "%");
//            }
//            //品牌的ID
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getFormula())) {
//                criteria.andEqualTo("formula", drug.getFormula());
//            }
//            //品牌的排序
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getDosage())) {
//                criteria.andEqualTo("dosage", drug.getDosage());
//            }
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getPrice())) {
//                criteria.andEqualTo("price", drug.getPrice());
//            }
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getSupplier())) {
//                criteria.andEqualTo("supplier", drug.getSupplier());
//            }
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getProducttime())) {
//                criteria.andEqualTo("producttime", drug.getProducttime());
//            }
//            if (!org.springframework.util.StringUtils.isEmpty(drug.getStock())) {
//                criteria.andEqualTo("stock", drug.getStock());
//            }
//
//        }
//        return example;
//    }


}
